/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.gestion.exception.SolicitudException;
import co.gov.medellin.sdep.sisdep.model.FeriaCritValoracion;
import co.gov.medellin.sdep.sisdep.model.FeriaEvalPart;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaCritValoracionException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaEvalPartException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaEvalPartRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.service.IFeriaEvalPartService;

/**
 * @author Usuario
 * 
 */
public class FeriaEvalPartController extends SolicitudBaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public FeriaEvalPartController() {
		this.feriaEvalPartListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda FeriaEvalPart */
	public FeriaEvalPart feriaEvalPartSearch = new FeriaEvalPart();

	/** Objeto contenedor del listado de FeriaEvalPart luego de una consulta. */
	private transient DataModel feriaEvalPartListModel;

	/** bean para el manejo del formulario de creacion/edicion. FeriaEvalPart */
	public FeriaEvalPart feriaEvalPart = new FeriaEvalPart();

	/** instancia de servicio feriaEvalPartService */
	private IFeriaEvalPartService feriaEvalPartService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	public String initState() throws FeriaEvalPartException {
		Map params = getExternalContext().getRequestParameterMap();
		Long solicitudId = new Long((String) params.get("solicitudId"));
		Long feriaId = new Long((String) params.get("feriaId"));
		this.solicitudOrigenRedirectPage = (String) params.get("solicitudOrigenRedirectPage");
		this.solicitudDestinoRedirectPage = (String) params.get("solicitudDestinoRedirectPage");
		//cleanSearchValues();
		//this.solicitud = getSolicitudService().get(solicitudId.longValue());
		//this.evento = getEventoService().getEventoRelatedBy(solicitudId.longValue());
		this.feriaEvalPart = getFeriaEvalPartService().getFeriaEvalPartRelatedBy(feriaId.longValue());
		if(feriaId != null && this.feriaEvalPart == null){
			this.feriaEvalPart = new FeriaEvalPart();
			//cleanValues();
		}
		this.feriaEvalPart.setIdFeriaArtesanal(feriaId);
		return solicitudOrigenRedirectPage;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchFeriaEvalPartByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getFeriaEvalPartService().load(1, 10,
					this.feriaEvalPartSearch);
			this.feriaEvalPartListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaEvalPartListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaEvalPartListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIAEVALPART_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public int searchFeriaEvalPartByIdFeria(Long idFeriaArtesanal,IFeriaEvalPartService service) {
		String msg = "";
		int intRdo=0;
		try {
			List valores = null;
			FeriaEvalPart feriaCritEvalPart = new FeriaEvalPart();
			feriaCritEvalPart.setIdFeriaArtesanal(idFeriaArtesanal);
			setFeriaEvalPartService(service);
			valores = getFeriaEvalPartService().loadByIdFeria(1, 10,
					feriaCritEvalPart);
			this.feriaEvalPartListModel.setWrappedData(valores);
			if(valores != null)
				intRdo = valores.size();
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaEvalPartListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaEvalPartListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		//((MessageModalWindowController) getBean("messageController"))
		//		.setSuccessMessage(msg, null, null);
		return intRdo;
	}

	/**
	 * 
	 * @return
	 */

	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaEvalPart.getId().longValue() == 0L) {
				getFeriaEvalPartService().insert(this.feriaEvalPart);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (FeriaEvalPartException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaEvalPartRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Feria Valoraci\u00F3n conclusiones " + msg, null, null);
		return AppConstants.FERIAEVALPART_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaEvalPart.getId() == null) {
				getFeriaEvalPartService().insert(this.feriaEvalPart);
				msg = AppConstants.SUCCESS_CREATE;
			}
			if (this.feriaEvalPart.getId() != null) {
				getFeriaEvalPartService().update(this.feriaEvalPart);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (FeriaEvalPartException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaEvalPartRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Feria Valoraci\u00F3n conclusiones " + msg, null, null);
		return this.solicitudDestinoRedirectPage;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createFeriaEvalPart() {
		feriaEvalPart = new FeriaEvalPart();
		feriaEvalPart.setId(new Long(0L));
		//feriaEvalPart.setCodigo("");
		//feriaEvalPart.setValor("");
		//feriaEvalPart.setActivo("N");
		this.solicitudDestinoRedirectPage = AppConstants.FERIAEVALPART_SEARCH_VIEW;
		return AppConstants.FERIAEVALPART_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateFeriaEvalPart() {
		Map params = getExternalContext().getRequestParameterMap();
		String feriaEvalPartDataIndex = (String) params
				.get("feriaEvalPartDataIndex");
		if(feriaEvalPartDataIndex == null)
			feriaEvalPartDataIndex="0";
		
		this.feriaEvalPart = (FeriaEvalPart) ((List) this.feriaEvalPartListModel
				.getWrappedData())
				.get(Integer.parseInt(feriaEvalPartDataIndex));
		this.solicitudDestinoRedirectPage = AppConstants.FERIAEVALPART_SEARCH_VIEW;
		return AppConstants.FERIAEVALPART_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelFeriaEvalPart() {
		return AppConstants.FERIAEVALPART_SEARCH_VIEW;
	}
	/**
	 * 
	 * @return
	 */
	public String cancelFeriaEvalPartOpcion() {
		return AppConstants.FERIAEVALPART_MENU_VIEW;
	}
		
	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public FeriaEvalPart getFeriaEvalPartSearch() {
		return feriaEvalPartSearch;
	}

	/**
	 * 
	 * @param feriaEvalPartSearch
	 */
	public void setferiaEvalPartSearch(FeriaEvalPart feriaEvalPartSearch) {
		this.feriaEvalPartSearch = feriaEvalPartSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getferiaEvalPartListModel() {
		return feriaEvalPartListModel;
	}

	/**
	 * 
	 * @param feriaEvalPartListModel
	 */
	public void setferiaEvalPartListModel(DataModel feriaEvalPartListModel) {
		this.feriaEvalPartListModel = feriaEvalPartListModel;
	}

	/**
	 * 
	 * @return
	 */
	public FeriaEvalPart getFeriaEvalPart() {
		return feriaEvalPart;
	}

	/**
	 * 
	 * @param feriaEvalPart
	 */
	public void setferiaEvalPart(FeriaEvalPart feriaEvalPart) {
		this.feriaEvalPart = feriaEvalPart;
	}

	/**
	 * obtencion del servicio feriaEvalPartService
	 * 
	 * @return
	 */
	public IFeriaEvalPartService getFeriaEvalPartService() {
		return feriaEvalPartService;
	}

	/**
	 * establecimiento del servicio feriaEvalPartService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param feriaEvalPartService
	 */
	public void setFeriaEvalPartService(
			IFeriaEvalPartService feriaEvalPartService) {
		this.feriaEvalPartService = feriaEvalPartService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
